package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

/**
 * @Title: autoGetUserInfo* @Description: 定时调用，将客户信息表中的数据汇总至customer中
 * @author: tian @date 2021-12-28 14:00:00
 */

class auto5GSMSTemplate implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(auto5GSMSTemplate.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {

        try {
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
            // 获取发送所需要的Token
            String DOMAIN = KHDBConfig.getAPIURL(eliteBean, "EliteInner-API")
            JSONObject apiTokenJson = JSONObject.fromObject(Util.doHttpGet(DOMAIN + "/apiGetToken?appid=5GSMS", "UTF-8", 5000, 0));
            String access_token = apiTokenJson.getJSONObject("data").getString("access_token");

            // 查出传真发送的地址
            Map<String, String> headParams = new HashMap<String, String>();
            headParams.put("Content-Type", "application/json");
            headParams.put("Authorization", "Bearer " + access_token);
            headParams.put("serverRoot", KHDBConfig.getAPIURL(eliteBean, "5G-SMS-rootUrl"));

            String resultInfo = Util.doHttpGetUseHead(KHDBConfig.getAPIURL(eliteBean, "5G-SMS-templateUrl") + "?pageNum=1&pageSize=1000&name", "UTF-8", 120000, 0, headParams);
            log.debug("返回请求体：" + resultInfo);

            JSONObject resultJson = JSONObject.fromObject(resultInfo);
            JSONArray resultArr = resultJson.getJSONObject("data").getJSONArray("records");
            for (int i = 0; i < resultArr.size(); i++) {
                String id = resultArr.getJSONObject(i).getString("id");
                String name = resultArr.getJSONObject(i).getString("name");
                String sign = resultArr.getJSONObject(i).getString("sign");
                String creator = resultArr.getJSONObject(i).getString("creator");
                String createDate = resultArr.getJSONObject(i).getString("createDate");
                String modifier = resultArr.getJSONObject(i).getString("modifier");
                String modifyDate = resultArr.getJSONObject(i).getString("modifyDate");
                String delFlag = resultArr.getJSONObject(i).getString("delFlag");
                String dynamicValues = resultArr.getJSONObject(i).getString("dynamicValues");

                String vpStatus = resultArr.getJSONObject(i).getString("vpStatus");
                String ydStatus = resultArr.getJSONObject(i).getString("ydStatus");
                String ltStatus = resultArr.getJSONObject(i).getString("ltStatus");
                String dxStatus = resultArr.getJSONObject(i).getString("dxStatus");
                String auditStatus = vpStatus + "/" + ydStatus + "/" + ltStatus + "/" + dxStatus;

                String mmsbody = resultArr.getJSONObject(i).getJSONObject("content").getString("subject");

                JSONArray mmsbodyArr = resultArr.getJSONObject(i).getJSONObject("content").getJSONArray("mmsbody");
                for (int j = 0; j < mmsbodyArr.size(); j++) {
                    mmsbody = mmsbody + System.lineSeparator() + mmsbodyArr.getJSONObject(j).getString("body");
                }
                log.debug("处理模板ID:" + id + " 模板名称:" + name + " 内容:" + mmsbody);

                String dealSQL = "select * from DocTemplate_5G where Doctemplate_Id = ?";
                List<Map<String, Object>> selectCus = jt.queryForList(dealSQL, id);

                log.debug("selectCus：" + selectCus);
                log.debug("检查数据(" + id + ") 数量：" + selectCus.size());

                if (selectCus.size() == 0) {
                    String insertSQL = "Insert Into DocTemplate_5G (Doctemplate_Id, Docgroup_Id, Description, Documentpath, Documentpath2, Status, Sms_Sign, Doctype, auditStatus, DynamicValues, Isparameter, Createrby, Creatertime, Modifiedby, Modifiedtime) Values (?, null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                    int num = jt.update(insertSQL, id, name, mmsbody, mmsbodyArr.toString(), delFlag, sign, "5", auditStatus, dynamicValues, mmsbodyArr.size(), creator, createDate, modifier, modifyDate);
                    log.debug("新增数据(" + id + ") 数量：" + num);
                } else {
                    String updateSQL = "Update DocTemplate_5G Set Description = ?, Documentpath = ?, Documentpath2 = ?, Status = ?, Sms_Sign = ?, auditStatus = ?, Modifiedby = ?, Modifiedtime = ? Where Doctemplate_Id = ?";
                    int num = jt.update(updateSQL, name, mmsbody, mmsbodyArr.toString(), delFlag, sign, auditStatus, modifier, modifyDate, id);
                    log.debug("更新数据(" + id + ") 数量：" + num);
                }

            }

        } catch (Exception e) {
            log.error("自动获取短信平台模板异常-:" + e.getMessage());
        }
    }
}